package vip.xumy.admin.bmap.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import vip.xumy.admin.bmap.pojo.CityLocate;
import vip.xumy.admin.bmap.pojo.DownWorker;
import vip.xumy.admin.bmap.pojo.WorkContext;
import vip.xumy.admin.bmap.service.CityLocateService;
import vip.xumy.admin.bmap.service.LoadWorkService;
import vip.xumy.admin.bmap.util.DownloadWorker;
import vip.xumy.core.pojo.com.AjaxResponse;
import vip.xumy.core.pojo.com.PageResponse;

/**	
 *	@author:mengyxu
 *	@date:2021年10月25日
 */

@RestController
@RequestMapping("bmap")
public class BMapController {
	@Autowired
	private CityLocateService cityLocateService;
	@Autowired
	private LoadWorkService loadWorkService;
	@Autowired
	private DownloadWorker worker;
	
	@GetMapping("city/list")
	public PageResponse<CityLocate> list(CityLocate example){
		Page<CityLocate> pages = PageHelper.startPage(example.getPage(), example.getSize());
		List<CityLocate> list = cityLocateService.list(example);
		PageResponse<CityLocate> rsp = new PageResponse<>();
		rsp.setRows(list);
		rsp.setTotal(pages.getTotal());
		return rsp;
	}
	
	@PostMapping("work/add")
	public AjaxResponse add(@RequestBody WorkContext context) throws Exception {
		loadWorkService.add(context);
		return new AjaxResponse(true, "添加下载任务成功");
	}
	
	@GetMapping("work/history")
	public PageResponse<DownWorker> workHistory(int page, int size){
		Page<DownWorker> pages = PageHelper.startPage(page, size);
		List<DownWorker> list = loadWorkService.history();
		PageResponse<DownWorker> rsp = new PageResponse<>();
		rsp.setRows(list);
		rsp.setTotal(pages.getTotal());
		return rsp;
	}
	
	@GetMapping("work/wait")
	public List<DownWorker> waitWork(){
		return loadWorkService.waitList();
	}
	
	@GetMapping("working")
	public WorkContext working(){
		return worker.getContext();
	}

}
